function getDayOfWeek(dayNum) {
	var dayResult;
	if(dayNum == 0) {
		dayResult = '日';
	} else if(dayNum == 1) {
		dayResult = '一';
	} else if(dayNum == 2) {
		dayResult = '二';
	} else if(dayNum == 3) {
		dayResult = '三';
	} else if(dayNum == 4) {
		dayResult = '四';
	} else if(dayNum == 5) {
		dayResult = '五';
	} else if(dayNum == 6) {
		dayResult = '六';
	}
	return dayResult;
}

function getDateByMonth(writeTime) {
	var date = new Date(writeTime);
	var yearNum = date.getFullYear();
	var monthNum = date.getMonth() + 1;
	var dateList = [];
	if(monthNum < 8) {
		if(monthNum == 2) {
			if(yearNum % 4 == 0) {
				for(var i = 1; i <= 29; i++) {
					dateList.push(i);
				}
			} else {
				for(var i = 1; i <= 28; i++) {
					dateList.push(i);
				}
			}
		} else {
			if(monthNum % 2 == 0) {
				for(var i = 1; i <= 30; i++) {
					dateList.push(i);
				}
			} else {
				for(var i = 1; i <= 31; i++) {
					dateList.push(i);
				}
			}
		}
	} else {
		if(monthNum % 2 == 0) {
			for(var i = 1; i <= 31; i++) {
				dateList.push(i);
			}
		} else {
			for(var i = 1; i <= 30; i++) {
				dateList.push(i);
			}
		}
	}
	return dateList;
}

function updateTable(writeTime, studentList, workDateList, disciplineList, disciplineRecordList) {
	var time;
	var recordTime;
	var dateNum;
	var dayNum;
	var studentId;
	var disciplineCount = 0;
	var deductSum = 0;
	var className = $("#className").val();
	var workDateNum = 0;
	var htmlText = new String();
	var dateList = [];
	var disciplineTime;
	var p = 0;
	var x = 0;
	var y = 0;
	var n = 0;

	// 清空数据
	$('#dateTime').html('');
	$('#accountClass').html('');
	$('#disciplineTable thead').empty();
	$('#disciplineTable tbody').empty();

	// 创建表头信息
	$('#dateTime').html(writeTime);
	$('#accountClass').html(className);

	// 创建时间表
	htmlText = '<tr><td rowspan="2" class="tdContent">姓名</td>';
	dateList = getDateByMonth(writeTime);

	if(workDateList) {
		for(var i = 0; i < dateList.length; i++) {
			if(dateList[i] < 10) {
				recordTime = writeTime + "-0" + dateList[i];
			} else {
				recordTime = writeTime + "-" + dateList[i];
			}
			time = new Date(recordTime);
			dayNum = time.getDay();
			dateNum = time.getDate();
			workDateNum = new Date(workDateList[p]).getDate();
			if(dateNum == workDateNum) {
				htmlText = htmlText + '<td class="tdContent">' + getDayOfWeek(dayNum) + '</td>';
				p++;
			} else {
				htmlText = htmlText + '<td class="isRecess">' + getDayOfWeek(dayNum) + '</td>';
			}
		}
	}

	htmlText = htmlText + '<td rowspan="2" class="tdContent">汇总</td><td rowspan="2" class="tdContent">累计</td></tr><tr>';

	p = 0;
	if(workDateList) {
		for(var i = 0; i < dateList.length; i++) {
			if(dateList[i] < 10) {
				recordTime = writeTime + "-0" + dateList[i];
			} else {
				recordTime = writeTime + "-" + dateList[i];
			}
			time = new Date(recordTime);
			dateNum = time.getDate();
			workDateNum = new Date(workDateList[p]).getDate();
			if(dateNum == workDateNum) {
				htmlText = htmlText + '<td class="tdContent">' + dateList[i] + '</td>';
				p++;
			} else {
				htmlText = htmlText + '<td class="isRecess">' + dateList[i] + '</td>';
			}
		}
	}
	htmlText = htmlText + '</tr>';

	$('#disciplineTable thead').append(htmlText);
	htmlText = '';

	// 创建内容表格

		for(var i = 0; i < studentList.length; i++) {
			p = 0;
			htmlText = htmlText + '<tr><td class="tdContent">' + studentList[i].stu_name + '</td>';
			for(var j = 0; j < dateList.length; j++) {
				n = 0;
				if(dateList[j] < 10) {
					recordTime = writeTime + "-0" + dateList[j];
				} else {
					recordTime = writeTime + "-" + dateList[j];
				}
				time = new Date(recordTime);
				dateNum = time.getDate();
				workDateNum = new Date(workDateList[p]).getDate();
				if(dateNum == workDateNum) {
					htmlText = htmlText + '<td class="tdContent">';
					p++;
				} else {
					htmlText = htmlText + '<td class="isRecess">';
				}

				// 创建数据
				if(disciplineRecordList) {
				    for(var k=0;k<disciplineRecordList.length;k++){
						disciplineTime=new Date(disciplineRecordList[k].write_time).getTime();
					    if(disciplineRecordList[k].student_id==studentList[i].id&&
					       disciplineTime==time.getTime()){
					    	if(n>0){
					    	   htmlText = ","+htmlText+disciplineRecordList[k].discipline_id;
					    	}
					    	else
					    	{
					    	   htmlText = htmlText+disciplineRecordList[k].discipline_id;
					    	}
					    }
					    n++;
				    }
				}
				htmlText = htmlText + '</td>';
			}
			htmlText = htmlText + '<td class="tdContent"></td><td class="tdContent"></td></tr>';
		}

	// 创建违纪扣分信息
	if(dateList) {
		htmlText = htmlText + '</tr><tr><td colspan="' + dateList.length + 3 + '">';
	} else {
		htmlText = htmlText + '</tr><tr><td colspan="' + 3 + '">';
	}

	if(disciplineList) {
		for(var i = 0; i < disciplineList.length; i++) {
			htmlText = htmlText + disciplineList[i].disciplineId + '、' + disciplineList[i].disciplineName + '；扣' + disciplineList[i].deductNum + '分.  ';
		}
	}

	htmlText = htmlText + '</td></tr>';
	$('#disciplineTable tbody').append(htmlText);
}

